<?php
// +----------------------------------------------------------------------
//            -------------------------
//           /   / ----------------\  \
//          /   /             \  \
//         /   /              /  /
//        /   /    /-------------- /  /
//       /   /    /-------------------\  \
//      /   /                   \  \
//     /   /                     \  \
//    /   /                      /  /
//   /   /      /----------------------- /  /
//  /-----/      /---------------------------/
// +----------------------------------------------------------------------
// | Copyright (c) 2016 http://baimifan.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: Sage 2016/4/22  9:27
// +----------------------------------------------------------------------
namespace Admin\Controller;
use Common\Model\BookModel;
use Admin\Common\Page;
use Think\Model;

Class SearchSaleController extends CashierBaseController {

    function index() {
        $map = [];
        $pay_id = I('get.pay_id');
        if (!empty($pay_id)) {
            $map['a.pay_id'] = $pay_id;
        }
        $keywords = I('get.keywords');
        $type = I('get.type');
        $time = I('get.time');
        if (!empty($keywords)) {
            $map['a.pay_id'] = $keywords;
        }
        if (!empty($type) && $type != 0) {
            $map['a.type'] = $type;
        }
        if (!empty($time)) {
            $time = explode("~" , I('get.time'));
            $map['a.create_time'] = [['gt' , $time[0] . " 00:00:00"] , ['lt' , $time[1] . " 23:59:59"] , "and"];
        }
        $count = D('Pay')->alias('a')->where($map)->count();// 查询满足要求的总记录数
        $Page = new  Page($count , C('page.size'));// 实例化分页类 传入总记录数和每页显示的记录数(25)
        //        $where['keywords'] = $keywords;
        //        $where['type'] = $type;
        $show = $Page->show();// 分页显示输出
        $this->assign('page' , $show);// 赋值分页输出
        $dsum = D('Pay')->alias('a')->where($map)->field("sum(a.money) as money_total,sum(a.money_real) as money_real_total,sum(a.money_zero) as money_zero_total")->find();

        $dsum['money_fact_total']=$dsum['money_real_total']-$dsum['money_zero_total'];
        $dsum['money_cheap_total']=$dsum['money_total']-$dsum['money_fact_total'];

        $data = D('Pay')->alias('a')->where($map)
            ->join("left join card c on a.user_id=c.user_id")
            ->limit($Page->firstRow . ',' . $Page->listRows)->order('a.pay_id desc')->field("a.*,c.card_id")->select();
        if(!empty(I('get.excel')) && I('get.excel') == '1'){

            $data = D('Pay')->alias('a')->where($map)
                ->join("left join card c on a.user_id=c.user_id")
                ->order('a.pay_id desc')->field("a.*,c.card_id")->select();
            $file = excel_export($data);
            ouput_Browser($file);
        }else{
            $this->data = $data;
            $this->dsum = $dsum;
            $this->display();
        }
    }
}